package com.woniu.service.Impl.backpage;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.dao.backpage.OrdersDao;
import com.woniu.entity.Orders;
import com.woniu.service.backpage.IOrdersService;
import com.woniu.util.DateUtil;
import com.woniu.util.JWTUtil;
import com.woniu.util.RequestParams;
import com.woniu.util.ResData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 订单表实现类
 * @author 任楷锐
 * @data 2023/3/6 10:46
 */
@Service
public class OrdersServiceImpl implements IOrdersService {
    @Autowired
    private OrdersDao ordersDao;

    @Resource
    RedisTemplate<String,Object> redisTemplate;
    /*查询所有订单*/
    @Override
    public ResData<?> getAllOrders(RequestParams<Orders> requestParams ) {
        /*实现分页*/
        PageHelper.startPage(requestParams.getPageNo(),requestParams.getPageSize());
        List<Orders> orderList = ordersDao.findAllOrders(requestParams.getData().getMultiple(),
                requestParams.getData().getPayMethod(), requestParams.getData().getStatus(), requestParams.getData().getStartTime(), requestParams.getData().getEndTime());
        PageInfo<Orders> pageInfo = new PageInfo(orderList);
        return new ResData<>().ok(pageInfo);
    }

    /*数据看板——查看当前月份订单数*/
    @Override
    public ResData<?> queryMoonOrdersCount(Date startTime,
                                           Date endTime) {
        if(startTime == null && endTime == null ){
            Date nextDate = DateUtil.getNextDate();

            String date = DateUtil.getDateFormat();

            //下个月的一号
            List<Orders> ordersList = ordersDao.queryMoonOrdersCount(date, nextDate);
            /*获取当前月份的每一天*/
            List<Integer> dayList = ordersList.stream().map(e -> e.getList()).collect(Collectors.toList());
            /*获取当前月份每一天的订单数*/
            List<Integer> nums = ordersList.stream().map(e -> e.getNums()).collect(Collectors.toList());

            Map map = new HashMap<>();
            map.put("dayList",dayList);
            map.put("nums",nums);
            return new ResData<>().ok(map);

         }
        //传过来的时间不为空
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String strDate = simpleDateFormat.format(startTime);
        List<Orders> ordersList = ordersDao.queryMoonOrdersCount(strDate, endTime);
        /*获取当前月份的每一天*/
        List<Integer> dayList = ordersList.stream().map(e -> e.getList()).collect(Collectors.toList());
        /*获取当前月份每一天的订单数*/
        List<Integer> nums = ordersList.stream().map(e -> e.getNums()).collect(Collectors.toList());

        Map map = new HashMap<>();
        map.put("dayList",dayList);
        map.put("nums",nums);
        return new ResData<>().ok(map);
    }

    /*数据看板——查看当前月份成交金额*/
    @Override
    public ResData<?> queryMoonOrdersAmount(Date startTime,
                                            Date endTime) {

        if (startTime == null && endTime == null){

            String date = DateUtil.getDateFormat();

            //下个月的一号
                Date nextDate = DateUtil.getNextDate();
                List<Orders> ordersAmount = ordersDao.queryMoonOrdersAmount(date, nextDate);
                /*获取当前月份的每一天*/
                List<Integer> dayList = ordersAmount.stream().map(e -> e.getList()).collect(Collectors.toList());
                /*获取当前月份的每一天的成交金额*/
                List<Double> amount = ordersAmount.stream().map(e -> e.getAmount()).collect(Collectors.toList());
                Map map = new HashMap<>();
                map.put("dayList",dayList);
                map.put("amount",amount);
                //把查询出来的数据放到redis里面

                return new ResData<>().ok(map);
         }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String strDate = simpleDateFormat.format(startTime);
        List<Orders> ordersAmount = ordersDao.queryMoonOrdersAmount(strDate, endTime);
        /*获取当前月份的每一天*/
        List<Integer> dayList = ordersAmount.stream().map(e -> e.getList()).collect(Collectors.toList());
        /*获取当前月份的每一天的成交金额*/
        List<Double> amount = ordersAmount.stream().map(e -> e.getAmount()).collect(Collectors.toList());
        Map map = new HashMap<>();
        map.put("dayList",dayList);
        map.put("amount",amount);
        return new ResData<>().ok(map);
    }

    /*数据看板——根据月份查看订单数量*/
    @Override
    public ResData<?> queryOrdersCountByCreateTime() {

            //当前月的一号
            Date date = DateUtil.getDate();
        //下个月的一号
            Date nextDate = DateUtil.getNextDate();
            //本个月的订单数
            Integer nowMoon = ordersDao.queryOrdersCountByCreateTime(date, nextDate);

            //上个月的一号
            Date lastDate = DateUtil.getLastDate();
            //上个月的订单数
            Integer lastMoon = ordersDao.queryOrdersCountByCreateTime(lastDate, date);
            //本月数据集合
            Map map = new HashMap<>();
            map.put("value",nowMoon);
            map.put("name","本月的订单数");

            Map map1 = new HashMap<>();
            map1.put("value",lastMoon);
            map1.put("name","上个月的订单数");

            Map[] btuData= {map,map1};
             return new ResData<>().ok(btuData);
        }

    @Override
    public ResData<?> queryAmountByCreateTime() {
        //当前月的一号
        Date date = DateUtil.getDate();
        //下个月的一号
        Date nextDate = DateUtil.getNextDate();
        //本个月的成交金额
        Double nowMoon = ordersDao.queryAmountByCreateTime(date, nextDate);
        Integer nowAmount = nowMoon.intValue();
        //上个月的一号
        Date lastDate = DateUtil.getLastDate();
        //上个月的成交金额
        Double lastMoon = ordersDao.queryAmountByCreateTime(lastDate, date);
        Integer lastAmount = lastMoon.intValue();

        //本月数据集合
        Map map = new HashMap<>();
        map.put("value",nowAmount);
        map.put("name","本月的成交金额");

        Map map1 = new HashMap<>();
        map1.put("value",lastAmount);
        map1.put("name","上个月的成交金额");

        Map[] bamData= {map,map1};
        return new ResData<>().ok(bamData);
    }


}
